package vista.paneles.hojaDibujo;

import java.awt.Point;
import java.util.Iterator;
import java.util.LinkedList;

import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;
import javax.swing.JComponent;

public class UtilHojaDeDibujo {

	private static int viewport[] = new int[4];
	private static float mvmatrix[] = new float[16];
	private static float projmatrix[] = new float[16];
	
    public static final GLU glu = new GLU();

	
	public static float[] getCoordPantalla(final GL2 gl, float xMundo, float yMundo)
	{
        float coordPantalla[] = new float[3];


		gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);
		gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, mvmatrix, 0);
		gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, projmatrix, 0);
      
		/*boolean test = */glu.gluProject(xMundo, yMundo, 0.0f,  mvmatrix, 0, projmatrix, 0,
						viewport, 0, coordPantalla, 0);
		
//		System.out.println("coord screen: "+ (int) coordPantalla[0] +" "+  (int) coordPantalla[1] + " " + test);
		
		return coordPantalla;
	}
	
	public static float[] getCoordMundo(final GL2 gl, int x, int y)
	{
		int realy = 0;// GL y coord pos
		float wcoord[] = new float[4];
		
		
		gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);
		gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, mvmatrix, 0);
		gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, projmatrix, 0);
        
		/* note viewport[3] is height of window in pixels */
		realy = viewport[3] - y - 1;

		glu.gluUnProject((float) x, (float) realy, 0.0f, mvmatrix, 0,
				projmatrix, 0, viewport, 0, wcoord, 0);
		
		return wcoord;
	}
	
	public static float[] getDimensionMundo(final GL2 gl, int x, int y)
	{
		float dimCoord[] = new float[4];
		
		gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);
		gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, mvmatrix, 0);
		gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, projmatrix, 0);
        
		glu.gluUnProject((float) x, (float) y, 0.0f, mvmatrix, 0,
				projmatrix, 0, viewport, 0, dimCoord, 0);

		return dimCoord;
	}
	
	public static float[] getCoordMundo(final GL2 gl, Point punto) {
		return UtilHojaDeDibujo.getCoordMundo(gl, punto.x, punto.y);
	}
	
    public static boolean estaContenido (JComponent contenedor, JComponent contenido) 
    {
    	int x1 = contenedor.getX();
    	int y1 = contenedor.getY();
    	int width1 = contenedor.getWidth();
    	int height1 = contenedor.getHeight();

    	int x2 = contenido.getX();
    	int y2 = contenido.getY();
    	int width2 = contenido.getWidth();
    	int height2 = contenido.getHeight();

    	if ( (x2 > x1) && (x2+width2 < x1+width1) && (y2 > y1) && (y2+height2 < y1 + height1))
    		return true;

    	return false;
    }
    
    public static boolean seSuperponeConAlgunComponente (JComponent componente, LinkedList<JComponent> lista )
    {
    	Iterator<JComponent> iter = lista.iterator();
    	boolean seSuperponen = false;

    	while(iter.hasNext()){  
    		JComponent otroComponente = iter.next();
    		if (componente != otroComponente) {             
    			if (UtilHojaDeDibujo.seSuperponenDosComponentes(componente, otroComponente)) {
    				seSuperponen = true;    
    			}
    		}
    	}
    	return seSuperponen; 
    }
    
    public static boolean seSuperponenDosComponentes (JComponent componente1, JComponent componente2) {
    	int x1 = componente1.getX();
    	int y1 = componente1.getY();
    	int width1 = componente1.getWidth();
    	int height1 = componente1.getHeight();

    	int x2 = componente2.getX();
    	int y2 = componente2.getY();
    	int width2 = componente2.getWidth();
    	int height2 = componente2.getHeight();

    	if ((x2+width2 > x1) && (x1+width1 > x2) && (y2+height2 > y1) && (y1+height1 > y2)) {
    		return true;
    	}
    	return false;
    }
}
